<?php
/**
 * +--------------------------------------------------------+
 * @Created by PhpStorm.
 * @Category StockController.php
 * @Depiction:
 * Author: hwy
 * Date: 2020/5/18  17:44
 * +--------------------------------------------------------+
 */

namespace App\Http\Controllers\Index;


use App\Http\Controllers\Controller;
use App\Services\StockService;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use App\Models\Stock;
use QL\QueryList;

class StockController extends Controller
{
    /**
     * @var stock 列表
     */
    protected $stock_list;
    /**
     * @var 导航
     */
    protected $nav;

    protected $get_url;

    protected $stock;

    public function __construct()
    {
        $this->stock      = StockService::getInstance();
        $this->get_url    = config('other.stock_url');
        $this->stock_list = StockService::getInstance()->getAll(2);
    }

    public function stockRealTime(Request $request)
    {
        $nav     = $this->nav;
        $compact = compact('nav');

        if (empty($request->type)) {
            return view('index.stock.stock_real_time', $compact);
        } else {
            return view('index.stock.stock_real_time_list', $compact);
        }

    }

    public function getStockRealTime(Request $request)
    {
        $t1       = microtime(true);
        $data     = $this->stock->get_stock_real_time($this->stock_list, $this->get_url);
        $data_cap = $this->stock->get_stock_real_time_cap($this->stock_list, $this->get_url);

        $indexs = $this->stock->getStockNum();

        foreach ($data as $key => $vo) {
            $list[] = array_merge($vo, $data_cap[$key]);
        }
        $t2 = microtime(true);

        //        dd(($t2-$t1)*1000);
        return response()->json(['code' => 1, 'data' => $list, 'indexs' => $indexs]);
    }


    public function stockForum(Request $request)
    {
        $nav = $this->nav;
        // $list = $this->stock_list;
        return view('index.stock.stock_forum', compact('nav', 'request'));
    }


    public function getStockForum(Request $request)
    {
        if (empty($request->stock)) {
            $request->stock = '002463';
        }

        if (!empty($request->number)) {
            $number         = explode('/', $request->number);
            $request->stock = $number[1] ?? '002463';
        }

        //需要采集的目标页面
        $page = 'http://guba.eastmoney.com/list,' . $request->stock . '.html';
        //采集规则
        $reg  = [
            //采集文章标题
            'title' => ['.a3 a', 'text'],
            'date'  => [
                '.a5',
                'text',
                '',
                function ($content) {
                    //                $arr = explode(' ',$content);
                    return $content;
                },
            ],
            'url'   => [
                '.a3 a',
                'href',
                '',
                function ($content) {
                    $url = 'https://guba.eastmoney.com' . $content;
                    return $url;
                },
            ],
            'pls'   => ['.a2', 'text'],
        ];
        $rang = '#articlelistnew .normal_post';
        $ql   = QueryList::get($page)->rules($reg)->range($rang)->query();
        $data = $ql->getData();

        $datas['data'] = $data->all();

        return response()->json(['code' => 0, 'data' => $datas]);
    }

    public function getStockForumInfo(Request $request)
    {
        $page    = $request->url;
        $compact = compact('request');

        return view('index.stock.stock_forum_info', $compact);
    }

    public function kLine(Request $request)
    {
        return view('index.stock._line', compact('request'));
    }


    public function getKLine(Request $request)
    {
        $code = $request->code ?: '000001';
        $type = $request->type ?: 60;

        if (substr($code, 0, 1) == 0) {
            $pre = 'sz';
        } else {
            $pre = 'sh';
        }
        $url = 'http://api.finance.ifeng.com/akmin?scode=' . $pre . $code . '&type=' . $type;

        $k_data = curl($url);
        $k_data = $data = iconv("gb2312", "utf-8//IGNORE", $k_data);
        $k_data = json_decode($k_data, true);

        return response()->json(['code' => 0, 'data' => $k_data['record']]);
    }


    public function costLine(Request $request)
    {

        return view('index.stock.cost_line', compact('request'));
    }

    public function getCostLine(Request $request)
    {
        $time    = Carbon::today()->addHours(9)->addMinutes(30);
        $data_jg = StockService::getInstance()->jgDataHandle($request->id, $time);

        $data_zlzj = StockService::getInstance()->zlzjDataHandle($request->id, $time);

        $data_pk      = StockService::getInstance()->pkDataHandle($request->id, $time);
        $data_pk_one  = $data_pk['retuen_data_one'];
        $data_pk_list = $data_pk['retuen_data'];

        $data_cjl = StockService::getInstance()->cjlDataHandle($request->id, $time);


        return response()->json([
            'code'         => 0,
            'data_jg'      => $data_jg,
            'data_zlzj'    => $data_zlzj,
            'data_pk_one'  => $data_pk_one,
            'data_pk_list' => $data_pk_list,
            'data_cjl'     => $data_cjl,
        ]);

    }


    public function getStockAll()
    {
        $responseData = $this->stock->getStockAll();
        return response()->json($responseData);

    }

    public function getPeData(Request $request)
    {
        $responseData = $this->stock->getPeData($request->id);
        return response()->json($responseData);

    }

    public function fundLoopBack()
    {
        return view('index.stock.fund_loop_back');
    }

    public function getFundLoopBack(Request $request)
    {
        $request->code = '008903';
        // $request->code = '110022'; // 4
        // $request->code = '003095'; //1
        // $request->code = '110011'; //4
        $code      = $request->code;
        $fund_data = Cache::remember('getFundLoopBack_' . $request->code, 20, function () use ($code) {
            return $fund_data = StockService::getInstance()->stockYnData(3, $code);
        });

        //回测

        $date = '6-15';

        // $date_n[]   = Carbon::parse('2017-'.$date)->toDateString(); // 39
        // $date_n[] = Carbon::parse('2018-'.$date)->toDateString(); // -312
        // $date_n[] = Carbon::parse('2019-'.$date)->toDateString(); //110
        $date_n[] = Carbon::parse('2020-'.$date)->toDateString();  // 341
        // $date_n[] = Carbon::parse('2021-6-10')->toDateString();
        dump($code);
        // $week = [4,8,12,20,24];
        $date_f = Carbon::parse('2019-'.$date)->toDateString();  // 341
        for ($d = 1; $d<2;$d++){
            $date_f = Carbon::parse($date_f)->addDay()->toDateString();
            $cost = 100;
            $i = 12;

            $data = StockService::getInstance()->fundLoopBack($fund_data, $date_f,$i, $cost);

            dump($data['data']);
        }

        dd('end3');



        $week = [4,8,12,20,24];
        foreach ($week as $v){
            foreach ($date_n as $value){
                $data = StockService::getInstance()->fundLoopBack($fund_data, $value,$v);
                dump($data['data']);

            };
            // dump('=============================');
        }




        dd('end');
        dd($fund_data);

    }


}
